import { Injectable, ViewChild } from '@angular/core';
import { Observable, of } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SharedMethodService {
  type:string =""
  title:string =""
  content:string =""

  constructor() { }
  cleared(checkedIds:any,tableData:any,datatable:any):Observable<any>{

    tableData.forEach((item:any) => {
      item.checked=false
      datatable.setTableCheckStatus(
        {
          pageAllChecked: false,
          pageHalfChecked: false,
        }
      );
    });
    return of(checkedIds=[]);
  }//清空表格选中状态
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; },datatable:any) {
    rowItem.$checked = checked;
    rowItem.$halfChecked = false;
    datatable.setRowCheckStatus({
      rowIndex: rowIndex,
      nestedIndex: nestedIndex,
      rowItem: rowItem,
      checked: checked
    });
  }

  getCheckedRows(datatable:any,id:any): Observable<any[]> {
    // 获取所有$checked属性为true的元素的id
    const rows = datatable.getCheckedRows();
    const checkedIds = rows.filter((item:any) => item.$checked).map((item:any) => item[id]);
    return of(checkedIds);
  }

  getCheckedRowsData(datatable:any): Observable<any[]> {
    // 获取所有$checked属性为true的元素
    const rows = datatable.getCheckedRows();
    const checkedRows = rows.filter((item:any) => item.$checked);
    return of(checkedRows);
  }
  handleError(error: any): Observable<any> {
    let errorInfo = { type: 'error', title: '错误', content: '' }; // 默认值

    switch (error.status) {
      case 400:
        errorInfo.content = '请检查表单是否填写正确';
        break;
      case 401:
        errorInfo.content = '请重新登录账号';
        break;
      case 403:
        errorInfo.content = '您没有权限进行此操作';
        break;
      case 404:
        errorInfo.content = '网络连接失败';
        break;
      default:
        errorInfo.content = error.error || '未知错误';
        break;
    }

    return of (errorInfo); // 返回错误信息对象
  }

}

